<template>
	<view>
		<ljy-address :address='address'></ljy-address>

		<view class="cart_title">
			<text class="iconfont icon-gouwuche"> 购物车</text>
		</view>

		<view>
			<ljy-goods v-for="(item,index) in cart" :key="index" :goods='item' showNumOnly="ture" @changeNum='changeNum'></ljy-goods>
		</view>
		<ljy-settle :buttonText="'支付'" :cartData='cart'></ljy-settle>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				cart: [],
				address: {}
			};
		},
		onShow() {
			//从缓存获取购物车数据
			// this.cart = uni.getStorageSync('cart') || [] || cart.filter(v => v.checked);

			//从缓存获取购物车数据
			this.address = uni.getStorageSync('address') || {};
			const buy = uni.getStorageSync('buy') || {};
			if (buy.goods_id) {
				this.cart = [];
				this.cart.push(buy);
				uni.removeStorageSync('buy');
			} else {
				this.cart = uni.getStorageSync('cart').filter(v => v.checked);
				// this.cart = this.cart.filter(v => v.checked);
			}

			//筛选购物车的数据
		},
		methods: {
			changeNum(e) {
				let index = this.cart.findIndex(v => v.goods_id == e.goods_id);

				if (e.cartNum === 0) {
					this.cart.splice(index, 1)
				} else {
					this.cart[index].cartNum = e.cartNum;
				}
				uni.setStorageSync('cart', this.cart);
			},

			changeAllChecked(e) {
				this.cart.forEach(v => v.checked = e)
			},
		}
	}
</script>

<style lang="scss">
	.cart_title {
		padding: 0 25rpx;
		border-bottom: 1px solid #cecece;
		
		text {
			font-size: 50rpx;
			color: #D9557C;
		}
	}
</style>